package com.umakr.ax.app.web;

import com.google.common.collect.Maps;
import com.umakr.ax.app.service.AppSmsService;
import com.umakr.ax.app.service.AppUserDetailsService;
import com.umakr.ax.common.vo.VerifyUserInfoVO;
import com.umakr.ax.core.base.AbstractController;
import com.umakr.ax.security.service.UserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/**
 * <p>
 * 用户详情  前端控制器
 * </p>
 *
 * @author Umakr-Code-Generator
 * @since 2017-12-30
 */
@Controller
@RequestMapping("/api/userDetails")
public class UserDetailsAppController extends AbstractController {
	@Autowired
	AppUserDetailsService userDetailsService;
	@Autowired
	AppSmsService appSmsService;
	@Autowired
    UserService userService;

	@ApiOperation(value = "用户注册")
	@GetMapping("/registerUser")
	@ResponseBody
	public Object registerUser(ModelMap modelMap, String phone,String code,String pwd,String nickName,Long userId) {
        Integer isCheck = appSmsService.checkVerifyCode(phone,code);
		if(isCheck == 1){
			return setSuccessModelMap(modelMap,userDetailsService.registerUser(phone,pwd,nickName,userId));
		}else{
			Map<String,Object> resultMap = Maps.newLinkedHashMap();
			resultMap.put("errorCode",0);
			resultMap.put("msg",isCheck ==0?"验证码错误":"2验证码失效");
			return setSuccessModelMap(modelMap,resultMap);
		}
	}

	@ApiOperation(value = "修改密码")
	@GetMapping("/modifyPassword")
	@ResponseBody
	public Object modifyPassword(ModelMap modelMap, String phone,String code,String oldPwd,String newPwd, String confirmPwd) {
        Object resultMap = null;
        if(code == null && oldPwd == null){
            resultMap = userDetailsService.modifyPwd(phone,null,newPwd,confirmPwd,true);
        }else{
			Integer isCheck = appSmsService.checkVerifyCode(phone,code);
            if(isCheck == 1){
                resultMap = userDetailsService.modifyPwd(phone,oldPwd,newPwd,confirmPwd,false );
            }else{
                Map<String,Object> resultMapx = Maps.newLinkedHashMap();
                resultMapx.put("errorCode",0);
				resultMapx.put("msg",isCheck ==0?"验证码错误":"2验证码失效");
                resultMap = resultMapx;
            }
        }
        return setSuccessModelMap(modelMap,resultMap);
	}

	/**
	 * 修改用户信息或者完善用户信息
	 * @param modelMap
     * @return
     */
	@ApiOperation(value = "更新用户信息")
	@GetMapping("/updateUserInfo")
	@ResponseBody
	public Object updateUserInfo(ModelMap modelMap, Long userId,String params) {
		return setSuccessModelMap(modelMap,userDetailsService.updateUserInfo(userId,params));
	}

	/**
	 * 实名认证
	 * @param modelMap
	 * @return
	 */
	@ApiOperation(value = "实名认证")
	@PostMapping("/realNameVerify")
	@ResponseBody
	public Object realNameVerified(ModelMap modelMap,  VerifyUserInfoVO vo) {
		return setSuccessModelMap(modelMap,userDetailsService.realNameVerified(vo));
	}

	/**
	 * 实名认证状态详情
	 * @param modelMap
	 * @return
	 */
	@ApiOperation(value = "实名认证状态详情")
	@GetMapping("/getRealNameVerifyInfo")
	public Object getRealNameVerifyInfo(ModelMap modelMap, Long userId) {
		return setSuccessModelMap(modelMap,userDetailsService.getRealNameVerifyInfo(userId));
	}

	/**
	 * 微信注册
	 * @param modelMap
	 * @return
	 */
	@ApiOperation(value = "微信注册")
	@PostMapping("/wxRegister")
	@ResponseBody
	public Object wxRegister(ModelMap modelMap, Long userId,String nickName,String openId,String headImgUrl,
							 Integer sex,String country,String city,String province,String unionid) {
		return setSuccessModelMap(modelMap,userDetailsService.wxRegister(userId,nickName,openId,headImgUrl,sex,country,city,province,unionid));
	}

	/**
	 * 本地文件上传 不上传至阿里云
	 * @param modelMap
	 * @return
	 */
	@ApiOperation(value = "本地文件上传")
	@RequestMapping("/fileUpload")
	@ResponseBody
	public Object fileUpload(ModelMap modelMap, Long userId, @RequestParam("file") MultipartFile file){
		return setSuccessModelMap(modelMap,userDetailsService.fileUpload(userId,file));
	}


}